using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

namespace CSettingsUI
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class PropertyConverterForm : System.Windows.Forms.Form , IPropertyConverterGui
	{
		#region members

		private System.Windows.Forms.ErrorProvider errorProvider1;  
        
		protected WRM.Windows.Forms.PropertyTree propertyTree;
		protected System.Windows.Forms.Button btnOk;
        protected System.Windows.Forms.Button btnCancel;
		#endregion
        private IContainer components;

		public PropertyConverterForm()
		{
			InitializeComponent();
		}


		#region Windows Form Designer generated code
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
            this.propertyTree = new WRM.Windows.Forms.PropertyTree();
            this.btnOk = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.propertyTree)).BeginInit();
            this.SuspendLayout();
            // 
            // errorProvider1
            // 
            this.errorProvider1.ContainerControl = this;
            // 
            // propertyTree
            // 
            this.propertyTree.AutoNavigate = true;
            this.propertyTree.Dock = System.Windows.Forms.DockStyle.Top;
            this.propertyTree.ImageIndex = 0;
            this.propertyTree.Indent = 19;
            this.propertyTree.Location = new System.Drawing.Point(0, 0);
            this.propertyTree.Name = "propertyTree";
            this.propertyTree.SelectedPaneNode = null;
            this.propertyTree.ShowLines = false;
            this.propertyTree.ShowPlusMinus = true;
            this.propertyTree.ShowRootLines = false;
            this.propertyTree.Size = new System.Drawing.Size(584, 296);
            this.propertyTree.SplitterColor = System.Drawing.SystemColors.AppWorkspace;
            this.propertyTree.SplitterLeft = 145;
            this.propertyTree.TabIndex = 0;
            this.propertyTree.PaneDeactivating += new WRM.Windows.Forms.PropertyTree.PaneDeactivatingEventHandler(this.propertyTree_PaneDeactivating);
            // 
            // btnOk
            // 
            this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOk.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnOk.Location = new System.Drawing.Point(416, 306);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new System.Drawing.Size(75, 23);
            this.btnOk.TabIndex = 1;
            this.btnOk.Text = "&OK";
            this.btnOk.Click += new System.EventHandler(this.btnOk_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnCancel.Location = new System.Drawing.Point(498, 306);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 2;
            this.btnCancel.Text = "&Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // PropertyConverterForm
            // 
            this.AcceptButton = this.btnOk;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(584, 342);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnOk);
            this.Controls.Add(this.propertyTree);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "PropertyConverterForm";
            this.Text = "Options";
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.propertyTree)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion
		
		#region protected
		protected virtual void OnAcceptChanges(System.ComponentModel.CancelEventArgs e)
		{			
		}
		#endregion

		#region events		
		protected void btnOk_Click(object sender, System.EventArgs e)
		{
			System.ComponentModel.CancelEventArgs cea = new System.ComponentModel.CancelEventArgs(false);
			OnAcceptChanges(cea);
			if(cea.Cancel) return;
			this.Close();			
		}
        protected void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
		#endregion

		#region IPropertyConverterGui Members
		public Control CreateGroup(string group)
		{
			string[] groups = group.Split('\\');
			WRM.Windows.Forms.PaneNodeCollection currentPaneCollection = propertyTree.PaneNodes;
            WRM.Windows.Forms.PropertyPane propertyPane = null;
			//
			for(int i1 = 0 ; i1 < groups.GetLength(0) ; i1++)
			{
				propertyPane = null;
				// serach in current level for group
				for(int i = 0 ; i < currentPaneCollection.Count ; i++ )
				{
                    WRM.Windows.Forms.PaneNode pane = currentPaneCollection[i];
					//
					if(pane.Text == groups[i1])
					{
						currentPaneCollection = pane.PaneNodes;
						propertyPane = pane.PropertyPane;						
						break;
					}
				}
				if(propertyPane==null)
				{
					//
                    propertyPane = new WRM.Windows.Forms.PropertyPane();
					//
					new CSettingsUI.FlowManager(propertyPane);
                    propertyPane.SelectedImageIndex = 1;
                    propertyPane.ImageIndex = 0;
					propertyPane.Name = groups[i1];
					
					propertyPane.Text = groups[i1];					
					//
					currentPaneCollection.Add(propertyPane);
                    
                    propertyPane.SelectedImageIndex = 1;
                    propertyPane.ImageIndex = 0;

                    //
					currentPaneCollection = propertyPane.PaneNodes;
				}
			}			
			return propertyPane;
		}
		public IPropertyConverterControl CreateControl(System.Reflection.PropertyInfo propertyInfo)
		{
            
            Debug.WriteLine("CreateControl");

			System.Type type = propertyInfo.PropertyType;
			Control ctl = null;
			if(type.Equals(typeof(string)))
			{
				ctl = new TextBoxOption();

			}
			else if((type.BaseType!=null) && (type.BaseType.Equals(typeof(Enum))))
			{
				if(type.GetCustomAttributes(typeof(FlagsAttribute),true).GetLength(0)>0)
				{
					ctl = new CheckedListBoxOption();
				} 
				else
				{
					ctl = new ComboBoxOption();
				}

			}
			else if(type.Equals(typeof(int)))
			{
				ctl = new NumericUpDownOption();
			}
            else if (type.Equals(typeof(Single)))
            {
                ctl = new TextBoxOption();
            }
			else if(type.Equals(typeof(DateTime)))
			{
				ctl = new DateTimePickerOption();
			}
			else if(type.Equals(typeof(Decimal)))
			{
				ctl = new NumericUpDownOption();
			}
			else if(type.Equals(typeof(bool)))
			{
				ctl = new CheckBoxOption();
			}
            else if (type.Equals(typeof(Color)))
            {
                ctl = new ColorPickerOption();
            }
            else if (type.IsClass || type.IsInterface)
            {
                //ctl = new ButtonOption();
                ctl = new TextBoxClassOption();
            }
            else
            {
                System.Diagnostics.Debug.Assert(false, "Could not create a control for the property type: " + type.Name);

            }
			return ctl as IPropertyConverterControl;
		}
		public void SetError(IPropertyConverterControl control, string message)
		{
			this.errorProvider1.SetError((Control) control, message);
		}
		#endregion

        private void propertyTree_PaneDeactivating(WRM.Windows.Forms.PropertyTree sender, WRM.Windows.Forms.PaneSelectionEventArgs psea)
        {

        }
	}
}
